#include <iostream>
#include <string>
#include <regex>

using namespace std;

int main()
{
    string str = "GET http://c.pki.goog/r/gsr1.crl?user=zhangsan&pass=12325 HTTP/1.1\r\n";
    // string str = "GET http://c.pki.goog/r/gsr1.crl HTTP/1.1\r\n";
    smatch matches;

    try {
        // 尝试编译正则表达式
        // regex e("(GET|POST|HEAD) ([^?]*).*");
        // regex e("(GET|POST|HEAD) ([^?]*)\\?(.*)\\&(.*) (.*)");
        regex e("(GET|POST|HEAD) ([^?]*)(?:\\?(.*))? (HTTP/1\\.[01])(?:\n|\r\n)?");
        // regex e("(GET|POST|HEAD) (.*) (HTTP/1\\.[01])(?:\n|\r\n)?");


        
        bool ret = regex_match(str, matches, e);
        if(!ret)
        {
            cerr << "正则匹配失败" << endl;
            return -1;
        }

        int i = 0;
        for(auto &s : matches)
        {
            cout << i++ << ": " << s << endl;
        }
    }
    catch (const regex_error& e) {
        cerr << "正则表达式错误: " << e.what() << endl;
        cerr << "错误代码: " << e.code() << endl;
        return -1;
    }

    return 0;
}
